package com.yphone.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.zzkj.yunphone.R;

public class StretchContainer extends FrameLayout {
    private int duration = 500;
    private boolean isFirstTouch = true;
    private boolean isFixed;
    private boolean isHorizontal;
    private boolean mIsDraging;
    private float mLastX;
    private float mLastY;
    private StretchScrollView mScrollView;
    private Scroller mScroller;
    private int mTouchSlop;
    private float maxY = 600.0f;
    private float ratio;

    public StretchContainer(Context context) {
        super(context);
        initView(context, null);
    }

    public StretchContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public StretchContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public void initView(Context context, AttributeSet attrs) {
        if (attrs != null) {
            this.ratio = context.obtainStyledAttributes(attrs, new int[]{R.attr.ratio}).getFloat(0, 1.0f);
        }
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mScroller = new Scroller(context, new DecelerateInterpolator());
    }

    /* access modifiers changed from: protected */
    public void onFinishInflate() {
        super.onFinishInflate();
        this.mScrollView = (StretchScrollView) getChildAt(0);
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (this.isFixed) {
            return super.dispatchTouchEvent(ev);
        }
        float currentY = ev.getY();
        float currentX = ev.getX();
        switch (ev.getAction()) {
            case 0:
                this.mLastY = currentY;
                this.mLastX = currentX;
                this.isFirstTouch = true;
                break;
            case 1:
                if (this.mIsDraging) {
                    int scrollY = getScrollY();
                    this.mScroller.startScroll(0, scrollY, 0, -scrollY, this.duration);
                    this.mIsDraging = false;
                    invalidate();
                    return super.dispatchTouchEvent(ev);
                }
                break;
            case 2:
                float distanceY = currentY - this.mLastY;
                float distanceX = currentX - this.mLastX;
                if (this.isFirstTouch) {
                    this.isFirstTouch = false;
                    if (Math.abs(distanceX) > Math.abs(distanceY)) {
                        this.isHorizontal = true;
                    } else {
                        this.isHorizontal = false;
                    }
                }
                if (!this.isHorizontal) {
                    if (!this.mIsDraging) {
                        if (Math.abs(distanceY) > ((float) this.mTouchSlop)) {
                            if (distanceY <= 0.0f) {
                                if (this.mScrollView.isScrolledToBottom()) {
                                    this.mLastY = currentY;
                                    this.mIsDraging = true;
                                    break;
                                }
                            } else if (this.mScrollView.isScrolledToTop()) {
                                this.mLastY = currentY;
                                this.mIsDraging = true;
                                break;
                            }
                        }
                    } else {
                        if (distanceY <= 0.0f) {
                            if (this.mScrollView.isScrolledToTop()) {
                                scrollTo(0, 0);
                                this.mIsDraging = false;
                                return super.dispatchTouchEvent(ev);
                            }
                        } else if (this.mScrollView.isScrolledToBottom()) {
                            scrollTo(0, 0);
                            this.mIsDraging = false;
                            return super.dispatchTouchEvent(ev);
                        }
                        scrollTo(0, (int) ((-distanceY) * this.ratio));
                        return true;
                    }
                } else {
                    return super.dispatchTouchEvent(ev);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public void computeScroll() {
        if (this.mScroller.computeScrollOffset()) {
            scrollTo(this.mScroller.getCurrX(), this.mScroller.getCurrY());
            postInvalidate();
        }
        super.computeScroll();
    }

    public void setFixed(boolean isFixed2) {
        this.isFixed = isFixed2;
    }
}